package com.xunwulian.dubbo.handler;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xunwulian.api.iface.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

//@ActiveProfiles("dev")
@Component
public class DubboProvider {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//
//    @Reference
//    private SmsAPI smsAPI;
//    @Reference
//    private CmebAPI cmebAPI;
//    @Reference
//    private AppAPI appAPI;
//    @Reference
//    private TransactionAPI transactionAPI;
//    @Reference
//    private UserAPI userAPI;
//    @Reference
//    private CardAPI cardAPI;
//    @Reference
//    private CardNodeAPI cardNodeAPI;
//    @Reference
//    private RoleAPI roleAPI;
//    @Reference
//    private SubscriberAPI subscriberAPI;
//    @Reference
//    private MenuAPI menuAPI;
//    @Reference
//    private ConfigAPI configAPI;

    public void runDubboServer(String appId, String registryAddress) {
        // 通过自定义main函数
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
//                "classpath:META-INF/ApplicationContext-dubbo.xml");
//        context.getEnvironment().setDefaultProfiles("dev");

//        SmsAPI smsAPI = (SmsAPI) context.getBean("smsAPI");
//        new Provider<>(smsAPI, SmsAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        CmebAPI cmebAPI = (CmebAPI) context.getBean("cmebAPI");
//        new Provider<>(cmebAPI, CmebAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);


//        AppAPI appAPI = (AppAPI) context.getBean("appAPI");
//        new Provider<>(appAPI, AppAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        TransactionAPI transactionAPI = (TransactionAPI) context.getBean("transactionAPI");
//        new Provider<>(transactionAPI, TransactionAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        UserAPI userAPI = (UserAPI) context.getBean("userAPI");
//        new Provider<>(userAPI, UserAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        CardAPI cardAPI = (CardAPI) context.getBean("cardAPI");
//        new Provider<>(cardAPI, CardAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        CardNodeAPI cardNodeAPI = (CardNodeAPI) context.getBean("cardNodeAPI");
//        new Provider<>(cardNodeAPI, CardNodeAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        RoleAPI roleAPI = (RoleAPI) context.getBean("roleAPI");
//        new Provider<>(roleAPI, RoleAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        SubscriberAPI subscriberAPI = (SubscriberAPI) context.getBean("subscriberAPI");
//        new Provider<>(subscriberAPI, SubscriberAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        MenuAPI menuAPI = (MenuAPI) context.getBean("menuAPI");
//        new Provider<>(menuAPI, MenuAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        ConfigAPI configAPI = (ConfigAPI) context.getBean("configAPI");
//        new Provider<>(configAPI, ConfigAPI.class, ConstantBean.XWL_GROUP, appId, registryAddress, 20880);

//        context.start();
        synchronized (DubboProvider.class) {
            while (true) {
                try {
                    DubboProvider.class.wait();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(DubboProvider.class);
        // 获取环境变量
        String appId = System.getenv("appId");
        if (appId == null || "".equals(appId)) {
            appId = "mock";
        }
        logger.info("appId:" + appId);
        String registryAddress = System.getenv("registry");
        if (registryAddress == null || "".equals(registryAddress)) {
            registryAddress = "zookeeper://127.0.0.1:2181";
        }
        logger.info("registry:" + registryAddress);

        new DubboProvider().runDubboServer(appId, registryAddress);
    }
}
